package zOthers;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public final class Log {

	private static final String LOG_FILE_NAME = "./log.txt";

	private static File file;
	private static FileWriter writer;

	/**
	 * The initiation method should only be called at MyStory.class
	 */
	public static void zInit(){
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Date date =new Date();

		try{
			file = new File(LOG_FILE_NAME);

			// Delete previous log
			if(file.exists())
				file.delete();
			// Create a new log file
			file.createNewFile();

			writer = new FileWriter(file);
			writer.write("\t\t****** My_Story Log ******\n");
			writer.write("\t\t\t\t\t\t"+dateFormat.format(date)+"\n\n");

			writer.close();

		}catch(IOException e){
			e.printStackTrace();
		}
		System.out.println("Log is Ready");
	}

	public static void info(String TAG, String info){
		try{

			writer = new FileWriter(file,true);

			writer.write("  (i) -- "+TAG+" : "+info+"\n");

			writer.close();
		}catch(IOException e){
			e.printStackTrace();
		}
	}

	public static void error(String TAG, String error){
		try{

			writer = new FileWriter(file,true);

			writer.write("(e) -- "+TAG+" : "+error+"\n");

			writer.close();
		}catch(IOException e){
			e.printStackTrace();
		}
	}

	public static void warning(String TAG, String warn){
		try{

			writer = new FileWriter(file,true);

			writer.write(" (w) -- "+TAG+" : "+warn+"\n");

			writer.close();
		}catch(IOException e){
			e.printStackTrace();
		}
	}
}
